home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / LINUX / MATH_EMU.ZIP / MATH_EMU / FPU_FSCA.C < prev    next >
Encoding:
C/C++ Source or Header  |  1979-12-31  |  12.5 KB  |  339 lines

  1. /*        $NetBSD$  */
  2.  
  3. /*
  4.  * Copyright (c) 1995 Ken Nakata
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  * 1. Redistributions of source code must retain the above copyright
  11.  *    notice, this list of conditions and the following disclaimer.
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in the
  14.  *    documentation and/or other materials provided with the distribution.
  15.  * 3. The name of the author may not be used to endorse or promote products
  16.  *    derived from this software without specific prior written permission.
  17.  * 4. All advertising materials mentioning features or use of this software
  18.  *    must display the following acknowledgement:
  19.  *      This product includes software developed by Gordon Ross
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  22.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  23.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  25.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  30.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31.  */
  32.  
  33. /*
  34.  * FSCALE - separated from the other type0 arithmetic instructions
  35.  * for performance reason; maybe unnecessary, but FSCALE assumes
  36.  * the source operand be an integer.  It performs type conversion
  37.  * only if the source operand is *not* an integer.
  38.  */
  39.  
  40. #include "types.h"
  41. #include "signal.h"
  42. #include "frame.h"
  43.  
  44. #include "fpu_emul.h"
  45.  
  46. int
  47. fpu_emul_fscale(fe, insn)
  48.      struct fpemu *fe;
  49.      struct instruction *insn;
  50. {
  51.     struct frame *frame;
  52.     u_int *fpregs;
  53.     int word1, sig;
  54.     int regnum, format;
  55.     int scale, sign, exp;
  56.     u_int m0, m1;
  57.     u_int buf[3], fpsr;
  58.     int flags;
  59.     char regname;
  60.  
  61.     sig = 0;
  62.     frame = fe->fe_frame;
  63.     fpregs = &(fe->fe_fpframe->fpf_regs[0]);
  64.     /* clear all exceptions and conditions */
  65.     fpsr = fe->fe_fpsr & ~FPSR_EXCP & ~FPSR_CCB;
  66. #ifdef DEBUG
  67.     if (fpu_debug_level & DL_FSCALE) {
  68.           printf("  fpu_emul_fscale: FPSR = %08x, FPCR = %08x\n", fpsr, fe->fe_fpcr);
  69.     }
  70. #endif
  71.     word1 = insn->is_word1;
  72.     format = (word1 >> 10) & 7;
  73.     regnum = (word1 >> 7) & 7;
  74.  
  75.     fe->fe_fpcr &= FPCR_ROUND;
  76.     fe->fe_fpcr |= FPCR_ZERO;
  77.  
  78.     /* get the source operand */
  79.     if ((word1 & 0x4000) == 0) {
  80. #ifdef DEBUG
  81.           if (fpu_debug_level & DL_FSCALE) {
  82.               printf("  fpu_emul_fscale: FP%d op FP%d => FP%d\n",
  83.                        format, regnum, regnum);
  84.           }
  85. #endif
  86.           /* the operand is an FP reg */
  87. #ifdef DEBUG
  88.           if (fpu_debug_level & DL_FSCALE) {
  89.               printf("  fpu_emul_scale: src opr FP%d=%08x%08x%08x\n",
  90.                        format, fpregs[format*3], fpregs[format*3+1],
  91.                        fpregs[format*3+2]);
  92.           }
  93. #endif
  94.           fpu_explode(fe, &fe->fe_f2, FTYPE_EXT, &fpregs[format * 3]);
  95.           fpu_implode(fe, &fe->fe_f2, FTYPE_LNG, buf);
  96.     } else {
  97.           /* the operand is in memory */
  98.           if (format == FTYPE_DBL) {
  99.               insn->is_datasize = 8;
  100.           } else if (format == FTYPE_SNG || format == FTYPE_LNG) {
  101.               insn->is_datasize = 4;
  102.           } else if (format == FTYPE_WRD) {
  103.               insn->is_datasize = 2;
  104.           } else if (format == FTYPE_BYT) {
  105.               insn->is_datasize = 1;
  106.           } else if (format == FTYPE_EXT) {
  107.               insn->is_datasize = 12;
  108.           } else {
  109.               /* invalid or unsupported operand format */
  110.               sig = SIGFPE;
  111.               return sig;
  112.           }
  113.  
  114.           /* Get effective address. (modreg=opcode&077) */
  115.           sig = fpu_decode_ea(frame, insn, &insn->is_ea0, insn->is_opcode);
  116.           if (sig) {
  117. #ifdef DEBUG
  118.               if (fpu_debug_level & DL_FSCALE) {
  119.                     printf("  fpu_emul_fscale: error in decode_ea\n");
  120.               }
  121. #endif
  122.               return sig;
  123.           }
  124. #ifdef DEBUG
  125.           if (fpu_debug_level & DL_FSCALE) {
  126.               printf("  fpu_emul_fscale: addr mode = ");
  127.               flags = insn->is_ea0.ea_flags;
  128.               regname = (insn->is_ea0.ea_regnum & 8) ? 'a' : 'd';
  129.  
  130.               if (flags & EA_DIRECT) {
  131.                     printf("%c%d\n", regname, insn->is_ea0.ea_regnum & 7);
  132.               } else if (insn->is_ea0.ea_flags & EA_PREDECR) {
  133.                     printf("%c%d@-\n", regname, insn->is_ea0.ea_regnum & 7);
  134.               } else if (insn->is_ea0.ea_flags & EA_POSTINCR) {
  135.                     printf("%c%d@+\n", regname, insn->is_ea0.ea_regnum & 7);
  136.               } else if (insn->is_ea0.ea_flags & EA_OFFSET) {
  137.                     printf("%c%d@(%d)\n", regname, insn->is_ea0.ea_regnum & 7,
  138.                            insn->is_ea0.ea_offset);
  139.               } else if (insn->is_ea0.ea_flags & EA_INDEXED) {
  140.                     printf("%c%d@(...)\n", regname, insn->is_ea0.ea_regnum & 7);
  141.               } else if (insn->is_ea0.ea_flags & EA_ABS) {
  142.                     printf("0x%08x\n", insn->is_ea0.ea_absaddr);
  143.               } else if (insn->is_ea0.ea_flags & EA_PC_REL) {
  144.                     printf("pc@(%d)\n", insn->is_ea0.ea_offset);
  145.               } else if (flags & EA_IMMED) {
  146.                     printf("#0x%08x%08x%08x\n",
  147.                            insn->is_ea0.ea_immed[0], insn->is_ea0.ea_immed[1],
  148.                            insn->is_ea0.ea_immed[2]);
  149.               } else {
  150.                     printf("%c%d@\n", regname, insn->is_ea0.ea_regnum & 7);
  151.               }
  152.           }
  153. #endif
  154.           fpu_load_ea(frame, insn, &insn->is_ea0, (char*)buf);
  155. #ifdef DEBUG
  156.           if (fpu_debug_level & DL_FSCALE) {
  157.               printf(" fpu_emul_fscale: src = %08x%08x%08x, siz = %d\n",
  158.                        buf[0], buf[1], buf[2], insn->is_datasize);
  159.           }
  160. #endif
  161.           if (format == FTYPE_LNG) {
  162.               /* nothing */
  163.           } else if (format == FTYPE_WRD) {
  164.               /* sign-extend */
  165.               scale = buf[0] & 0xffff;
  166.               if (scale & 0x8000) {
  167.                     scale |= 0xffff0000;
  168.               }
  169.           } else if (format == FTYPE_BYT) {
  170.               /* sign-extend */
  171.               scale = buf[0] & 0xff;
  172.               if (scale & 0x80) {
  173.                     scale |= 0xffffff00;
  174.               }
  175.           } else if (format == FTYPE_DBL || format == FTYPE_SNG ||
  176.                        format == FTYPE_EXT) {
  177.               fpu_explode(fe, &fe->fe_f2, format, buf);
  178.               fpu_implode(fe, &fe->fe_f2, FTYPE_LNG, buf);
  179.           }
  180.           /* make it look like we've got an FP oprand */
  181.           fe->fe_f2.fp_class = (buf[0] == 0) ? FPC_ZERO : FPC_NUM;
  182.     }
  183.  
  184.     /* assume there's no exception */
  185.     sig = 0;
  186.  
  187.     /* it's barbaric but we're going to operate directly on
  188.      * the dst operand's bit pattern */
  189.     sign = fpregs[regnum * 3] & 0x80000000;
  190.     exp = (fpregs[regnum * 3] & 0x7fff0000) >> 16;
  191.     m0 = fpregs[regnum * 3 + 1];
  192.     m1 = fpregs[regnum * 3 + 2];
  193.  
  194.     switch (fe->fe_f2.fp_class) {
  195.     case FPC_SNAN:
  196.           fpsr |= FPSR_SNAN;
  197.     case FPC_QNAN:
  198.           /* dst = NaN */
  199.           exp = 0x7fff;
  200.           m0 = m1 = 0xffffffff;
  201.           break;
  202.     case FPC_ZERO:
  203.     case FPC_NUM:
  204.           if ((0 < exp && exp < 0x7fff) ||
  205.               (exp == 0 && (m0 | m1) != 0)) {
  206.               /* normal or denormal */
  207.               exp += scale;
  208.               if (exp < 0) {
  209.                     /* underflow */
  210.                     u_int grs;          /* guard, round and sticky */
  211.  
  212.                     exp = 0;
  213.                     grs = m1 << (32 + exp);
  214.                     m1 = m0 << (32 + exp) | m1 >> -exp;
  215.                     m0 >>= -exp;
  216.                     if (grs != 0) {
  217.                         fpsr |= FPSR_INEX2;
  218.  
  219.                         switch (fe->fe_fpcr & 0x30) {
  220.                         case FPCR_MINF:
  221.                               if (sign != 0) {
  222.                                   if (++m1 == 0 &&
  223.                                         ++m0 == 0) {
  224.                                         m0 = 0x80000000;
  225.                                         exp++;
  226.                                   }
  227.                               }
  228.                               break;
  229.                         case FPCR_NEAR:
  230.                               if (grs == 0x80000000) {
  231.                                   /* tie */
  232.                                   if ((m1 & 1) &&
  233.                                         ++m1 == 0 &&
  234.                                         ++m0 == 0) {
  235.                                         m0 = 0x80000000;
  236.                                         exp++;
  237.                                   }
  238.                               } else if (grs & 0x80000000) {
  239.                                   if (++m1 == 0 &&
  240.                                         ++m0 == 0) {
  241.                                         m0 = 0x80000000;
  242.                                         exp++;
  243.                                   }
  244.                               }
  245.                               break;
  246.                         case FPCR_PINF:
  247.                               if (sign == 0) {
  248.                                   if (++m1 == 0 &&
  249.                                         ++m0 == 0) {
  250.                                         m0 = 0x80000000;
  251.                                         exp++;
  252.                                   }
  253.                               }
  254.                               break;
  255.                         case FPCR_ZERO:
  256.                               break;
  257.                         }
  258.                     }
  259.                     if (exp == 0 && (m0 & 0x80000000) == 0) {
  260.                         fpsr |= FPSR_UNFL;
  261.                         if ((m0 | m1) == 0) {
  262.                               fpsr |= FPSR_ZERO;
  263.                         }
  264.                     }
  265.               } else if (exp >= 0x7fff) {
  266.                     /* overflow --> result = Inf */
  267.                     /* but first, try to normalize in case it's an unnormalized */
  268.                     while ((m0 & 0x80000000) == 0) {
  269.                         exp--;
  270.                         m0 = (m0 << 1) | (m1 >> 31);
  271.                         m1 = m1 << 1;
  272.                     }
  273.                     /* if it's still too large, then return Inf */
  274.                     if (exp >= 0x7fff) {
  275.                         exp = 0x7fff;
  276.                         m0 = m1 = 0;
  277.                         fpsr |= FPSR_OVFL | FPSR_INF;
  278.                     }
  279.               } else if ((m0 & 0x80000000) == 0) {
  280.                     /*
  281.                      * it's a denormal; we try to normalize but
  282.                      * result may and may not be a normal.
  283.                      */
  284.                     while (exp > 0 && (m0 & 0x80000000) == 0) {
  285.                         exp--;
  286.                         m0 = (m0 << 1) | (m1 >> 31);
  287.                         m1 = m1 << 1;
  288.                     }
  289.                     if ((m0 & 0x80000000) == 0) {
  290.                         fpsr |= FPSR_UNFL;
  291.                     }
  292.               } /* exp in range and mantissa normalized */
  293.           } else if (exp == 0 && m0 == 0 && m1 == 0) {
  294.               /* dst is Zero */
  295.               fpsr |= FPSR_ZERO;
  296.           } /* else we know exp == 0x7fff */
  297.           else if ((m0 | m1) == 0) {
  298.               fpsr |= FPSR_INF;
  299.           } else if ((m0 & 0x40000000) == 0) {
  300.               /* a signaling NaN */
  301.               fpsr |= FPSR_NAN | FPSR_SNAN;
  302.           } else {
  303.               /* a quiet NaN */
  304.               fpsr |= FPSR_NAN;
  305.           }
  306.           break;
  307.     case FPC_INF:
  308.           /* dst = NaN */
  309.           exp = 0x7fff;
  310.           m0 = m1 = 0xffffffff;
  311.           fpsr |= FPSR_OPERR | FPSR_NAN;
  312.           break;
  313.     default:
  314. #ifdef DEBUG
  315.           panic("  fpu_emul_fscale: invalid fp class");
  316. #endif
  317.           break;
  318.     }
  319.  
  320.     /* store the result */
  321.     fpregs[regnum * 3] = sign | (exp << 16);
  322.     fpregs[regnum * 3 + 1] = m0;
  323.     fpregs[regnum * 3 + 2] = m1;
  324.  
  325.     if (sign) {
  326.           fpsr |= FPSR_NEG;
  327.     }
  328.  
  329.     /* update fpsr according to the result of operation */
  330.     fe->fe_fpframe->fpf_fpsr = fe->fe_fpsr = fpsr;
  331. #ifdef DEBUG
  332.     if (fpu_debug_level & DL_FSCALE) {
  333.           printf("  fpu_emul_fscale: FPSR = %08x, FPCR = %08x\n",
  334.                  fe->fe_fpsr, fe->fe_fpcr);
  335.     }
  336. #endif
  337.     return (fpsr & fe->fe_fpcr & FPSR_EXCP) ? SIGFPE : sig;
  338. }
  339.